package com.xlx.openLink.controller;

import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.BaseMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.vo.RequestDevtLst;
import com.xlx.openLink.vo.RequestPortLst;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/dev")
@Slf4j
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 添加设备
     *
     * @param device
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public BaseDateMessage addDevice(@RequestBody Device device) {
        BaseDateMessage baseMessage = new BaseDateMessage();
        try {
            baseMessage.setData(deviceService.add(device));
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(e.getResultCodeEnum());
            baseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
        }
        return baseMessage;
    }


    /**
     * 初始化设备
     *
     * @param pkGuid
     * @return
     */
    @ResponseBody
    @GetMapping("/init")
    public BaseMessage initDev(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseMessage baseMessage = new BaseMessage();
        log.info("初始化设备---------");
        try {
            deviceService.initDev(pkGuid);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("初始化成功");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(e.getResultCodeEnum());
            baseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
        }
        return baseMessage;
    }


    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParam(name = "requestDevtLst", value = "分页请求参数", required = true, dataType = "RequestDevtLst")
    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestDevtLst requestDevtLst) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(deviceService.listByPage(requestDevtLst));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @ResponseBody
    @GetMapping("/del")
    public BaseMessage del(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            deviceService.delDev(pkGuid);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("删除设备成功");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(e.getResultCodeEnum());
            baseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
        }
        return baseMessage;
    }

    @ResponseBody
    @GetMapping("/select")
    public BaseDateMessage select(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseDateMessage baseMessage = new BaseDateMessage();
        try {
            baseMessage.setData(deviceService.select(pkGuid));
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(e.getResultCodeEnum());
            baseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
        }
        return baseMessage;
    }

    /**
     * 添加设备
     *
     * @param device
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public BaseMessage update(@RequestBody Device device) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            deviceService.update(device);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(e.getResultCodeEnum());
            baseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
        }
        return baseMessage;
    }

    @ResponseBody
    @GetMapping(value = "/initData")
    public BaseDateMessage initData() {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(deviceService.initData());
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("成功");

        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseDateMessage;
    }


}
